package com.hk.service.impl;

import com.hk.dao.IUserDao;
import com.hk.dao.impl.UserDaoImpl;
import com.hk.model.User;
import com.hk.service.IUserService;
import com.hk.utils.DateUtils;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/8 15:06
 * @Statement: 会员业务层
 */
public class UserServiceImpl implements IUserService {

    private IUserDao iUserDao = new UserDaoImpl();

    @Override
    public List<User> findAll(Integer page, Integer limit, User t) {
        ResultSet resultSet = iUserDao.findAll(page,limit,t);
        List<User> userList = packageListUser(resultSet);
//        关闭资源
        JdbcMysql.jdbcClose();
        return userList;
    }

    @Override
    public int count(Integer page, Integer limit, User t) {
        ResultSet resultSet = iUserDao.count(page,limit, t);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    /**
     * 接受查询数据并转化为对象
     * @param resultSet
     * @return
     */
    private List<User> packageListUser(ResultSet resultSet) {
        List<User> userList = new ArrayList<>(50);
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String username = resultSet.getString(2);
                String email = resultSet.getString(3);
                String password = resultSet.getString(4);
                int sex = resultSet.getInt(5);
                String birthday = resultSet.getString(6);
                String hobby = resultSet.getString(7);
                String phone = resultSet.getString(8);
                String site = resultSet.getString(9);
                String gmtCreate = resultSet.getString(10);
                String gmtModified = resultSet.getString(11);
                int state = resultSet.getInt(12);
                User user = new User(id,username,email,password,sex,birthday,hobby,phone,site,gmtCreate,gmtModified,state);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.getMessage();
        }
        return userList;
    }

    @Override
    public User findById(Integer id) {
        ResultSet resultSet = iUserDao.findById(id);
        List<User> userList = packageListUser(resultSet);
//        释放资源
        JdbcMysql.jdbcClose();
        return userList.get(0);
    }

    @Override
    public int save(User user) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtCreate(dateTimeString);
        user.setGmtModified(dateTimeString);
        int save = iUserDao.save(user);
//       关闭资源
        JdbcMysql.jdbcClose();
        if (save == 1) {
            System.out.println("添加成功");
        }
        return save;

    }

    @Override
    public int update(User user) {
//        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtModified(dateTimeString);
        int update = iUserDao.update(user);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (update == 1) {
            System.out.println("会员信息修改成功");
        }
        return update;
    }

    @Override
    public int updatePassWord(User user) {
//        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtModified(dateTimeString);
        int updatePassWord = iUserDao.updatePassWord(user);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (updatePassWord == 1) {
            System.out.println("会员密码修改成功");
        }
        return updatePassWord;
    }

    @Override
    public int updateState(User user) {
//        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtModified(dateTimeString);
        int updateState = iUserDao.updateState(user);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (updateState == 1) {
            System.out.println("状态修改成功");
        }
        return updateState;
    }

    @Override
    public int delete(Integer id) {
        int delete = iUserDao.delete(id);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (delete == 1) {
            System.out.println("会员删除成功");
        }
        return delete;
    }

    @Override
    public int findByEmail(String email) {
        ResultSet resultSet = iUserDao.findByEmail(email);
        List<User> userList = packageListUser(resultSet);
        //       关闭资源
        JdbcMysql.jdbcClose();
        if (userList.size() > 0) {
            System.out.println("邮箱已存在，请更换邮箱！");
            return 1;
        }
        return 0;
    }
}
